package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.constant.HttpResultEnum;
import com.dongdongshop.data.DataResult;
import com.dongdongshop.pojo.itemCat.TbItemCat;
import com.dongdongshop.service.itemCat.ItemCatService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Author Eayon
 * @Date 2020/2/28 9:36
 */
@Controller
@RequestMapping("item")
public class ItemCatController {

    @Reference
    private ItemCatService itemCatService;

    Logger log = LoggerFactory.getLogger(ItemCatController.class);

    /**
     * 根据父级id查询分类信息
     * @param pid
     * @return
     */
    @RequestMapping(value = "getItemCatByParentId",method = RequestMethod.POST)
    @ResponseBody
    public DataResult getItemCatByParentId(Long pid){
        List<TbItemCat> list = null;
        try {
            list = itemCatService.getItemCatByParentId(pid);
        }catch (Exception e){
            log.error("根据父级id查询分类信息异常",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage(),list);
    }

    /**
     * 根据分类id查询对应的模板id
     * @param id
     * @return
     */
    @RequestMapping(value = "getThisItemTypeId",method = RequestMethod.POST)
    @ResponseBody
    public DataResult getThisItemTypeId(Long id){
        TbItemCat ic = null;
        try {
            ic = itemCatService.getItemCatById(id);
        }catch (Exception e){
            log.error("根据分类id查询对应的模板id异常",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage(),ic);
    }
}
